package com.realgodo.site.web.admin.event;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.site.Event;
import com.realgodo.site.service.site.EventManager;
import com.realgodo.site.web.admin.AdminDefaultController;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/admin/event/") })
public class IndexController extends AdminDefaultController {

	private static final long serialVersionUID = -2785874156537348285L;

	@Autowired
	private EventManager eventManager;

	private Page<Event> eventPage = new Page<Event>(PAGE_SIZE);

	private Event event;

	private Long[] ids;

	public String execute() throws Exception {
		eventPage.setPageNo(eventManager.isParamNotNull(getParamMap(),
				Page.PAGE) ? Integer.valueOf((String) eventManager
				.getParamValue(getParamMap(), Page.PAGE)) : 1);// 查询页数
		this.eventPage = eventManager.findEvents(PAGE_SIZE, Event.OVER_ALL);
		super.setPageInfo(eventPage);
		return SUCCESS;
	}

	public String remove() throws Exception {
		if (ids.length != 0) {
			this.eventManager.removeEvent(ids);
		}
		return "toindex";
	}

	public String add() throws Exception {
		return "add";
	}

	public String save() throws Exception {
		this.eventManager.createEvent(event);
		return "toindex";
	}

	public String edit() throws Exception {
		if (null != getId()) {
			event = this.eventManager.get(getId());
			return "add";
		} else {
			return "toindex";
		}
	}

	public String top() throws Exception {
		if (null != getId()) {
			this.eventManager.topEvent(getId());
		}
		return "toindex";
	}

	public String over() throws Exception {
		if (null != getId()) {
			this.eventManager.overEvent(getId());
		}
		return "toindex";
	}

	public Page<Event> getEventPage() {
		return eventPage;
	}

	public void setEventPage(Page<Event> eventPage) {
		this.eventPage = eventPage;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
}
